/*******************************************************************************
 * Copyright (c) 2010 Marcelo Cataldo, Sangeeth Nambiar
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * LayersViewPart.java
 * 
 * View to select the enable and or visible status of different layers in the 
 * dependency graph
 * 
 * Contributors:
 *    Marcelo Cataldo  - initial implementation 
 *    Sangeeth Nambiar - initial implementation
 *******************************************************************************/
package edu.cmu.isr.dinvenio.ui.views;

import java.util.Enumeration;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.part.ViewPart;

import edu.cmu.isr.dinvenio.Activator;

public class LayersViewPart extends ViewPart {

	private Composite parent;
	private Group LayersGroup;

	public LayersViewPart() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		this.parent = parent;
		
		RowLayout l = new RowLayout(SWT.VERTICAL);
		l.fill = true;
		this.parent.setLayout(l);
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}
	public void addLayerIDs(Enumeration<String> layerIDs) {
		if (layerIDs != null){
			this.clearLayerIDs();
			
			LayersGroup = new Group(parent, SWT.NONE);
			LayersGroup.setText("Layers");
			RowLayout rl = new RowLayout(SWT.VERTICAL);
			rl.fill = true;
			LayersGroup.setLayout(rl);
			
			Label l = new Label(LayersGroup, SWT.NONE);
			l.setText("Visible  Enable");
			
			for (Enumeration<String> e = layerIDs ; e.hasMoreElements() ;) {    		        
				String buttonText = e.nextElement();
				
				Group layerGroup = new Group(LayersGroup, SWT.NONE);
				RowLayout layout = new RowLayout(SWT.HORIZONTAL);
				layout.marginLeft = 5;
				layout.spacing = 5;
				layerGroup.setLayout(layout);
				
				final Button buttonVis = new Button(layerGroup, SWT.CHECK);
				buttonVis.setData(buttonText);
		        buttonVis.setSelection(true);
		        buttonVis.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent event) {				
						Activator.myController.setLayerVisibility((String) buttonVis.getData(), buttonVis.getSelection());						
					}
				});
		        
				Label colLabel = new Label(layerGroup, SWT.NONE);
				colLabel.setText("    ");
				colLabel.setBackground(Activator.myController.getLayerColor(buttonText,this.parent.getDisplay()));
		        
				final Button buttonEna = new Button(layerGroup, SWT.CHECK);
				buttonEna.setData(buttonText);
				buttonEna.setSelection(true);
				buttonEna.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent event) {				
						if (buttonEna.getSelection() == true) {
							Activator.myController.enableLayer((String) buttonEna.getData(),buttonEna.getShell());
						}
						else {
							Activator.myController.disableLayer((String) buttonEna.getData(),buttonEna.getShell());
						}
					}
				});
				
				Label nameLabel = new Label(layerGroup, SWT.NONE);
				nameLabel.setText(buttonText);	
		     }	
		}
		refreshView();
	}
	private void clearLayerIDs(){
		if (LayersGroup != null){
			LayersGroup.dispose();
		}
	}
	public void refreshView(){
		parent.layout(true);
	}

}
